package com.alu4r.atcrowdfunding.manager.controller;

import com.alu4r.atcrowdfunding.bean.Role;
import com.alu4r.atcrowdfunding.bean.User;
import com.alu4r.atcrowdfunding.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * 用于跳转页面的控制器
 */

@Controller
@RequestMapping("/user")
public class UserToPageController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toAssignRole")
    public String toAssignRole(@RequestParam(value = "userId") Integer id, Map<String, Object> map){
        System.out.println("进入到角色分配页面");
        //查询所有所有角色
        List<Role> listRole = userService.queryAllRole();
        //用户角色表中，userId为id的用户角色id
        List<Integer> listRoleId = userService.queryRoleByUserId(id);

        List<Role> listLeftRole = new ArrayList<>();//左边未分配角色
        List<Role> listRightRole = new ArrayList<>();//右边已分配角色
        for(Role role : listRole){
            if(listRoleId.contains(role.getId())){
                listRightRole.add(role);
            }else {
                listLeftRole.add(role);
            }
        }
        map.put("listLeftRole",listLeftRole);
        map.put("listRightRole",listRightRole);
        return "user/assignRole";
    }

    @RequestMapping("/toEdit")
    public String toEdit(@RequestParam(value = "userId") Integer id, Map map) {
        System.out.println("后台主进入修改页面");
        User user = userService.getUser(id);
        map.put("user", user);
        return "user/edit";
    }

    @RequestMapping("/toIndex")
    public String toIndex() {
        System.out.println("后台主页面");
        return "user/index";
    }


    @RequestMapping("/toAdd")
    public String toAdd() {
        System.out.println("进入添加用户页面");
        return "user/add";
    }

}
